package nc.vo.ic.m4c.deal;

import java.util.ArrayList;
import java.util.List;
import nc.vo.ic.m4c.entity.SaleOutBodyVO;
import nc.vo.ic.m4c.entity.SaleOutHeadVO;
import nc.vo.ic.m4c.entity.SaleOutVO;
import nc.vo.ic.pub.util.CollectionUtils;
import nc.vo.ic.pub.util.NCBaseTypeUtils;
import nc.vo.ic.pub.util.ValueCheckUtil;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.scmpub.res.billtype.SOBillType;

public class Bill4CSplitProcess
{
  public SaleOutVO[] processSplitAndReplen(SaleOutVO[] vos) {
    SaleOutVO[] retvos = splitBills(vos);
    processBillRule(retvos);
    return retvos;
  }




  
  public SaleOutVO[] splitBills(SaleOutVO[] vos) { return splitBillByNums(vos); }


  
  public SaleOutVO[] processBillRule(SaleOutVO[] vos) {
    processFreplenishflag(vos);
    return vos;
  }



  
  private void processFreplenishflag(SaleOutVO[] vos) {
    for (SaleOutVO vo : vos) {
      if (SOBillType.Order.getCode().equals(vo.getBody(0).getCfirsttype()))
      {
        
        if (NCBaseTypeUtils.isLtZero(vo.getBody(0).getNshouldnum())) {
          vo.getHead().setFreplenishflag(UFBoolean.TRUE);
        }
      }
    } 
  }





  
  private SaleOutVO[] splitBillByNums(SaleOutVO[] vos) {
    List<SaleOutVO> retList = new ArrayList<SaleOutVO>();
    for (SaleOutVO vo : vos) {
      SaleOutBodyVO[] bodys = vo.getBodys();
      //
      if (!SOBillType.Order.getCode().equals(bodys[0].getCfirsttype())) {
        retList.add(vo);
      } else {
        
        List<SaleOutBodyVO> commBody = new ArrayList<SaleOutBodyVO>();
        List<SaleOutBodyVO> retBody = new ArrayList<SaleOutBodyVO>();
        for (SaleOutBodyVO body : bodys) {
          if (NCBaseTypeUtils.isLtZero(body.getNshouldnum())) {
            retBody.add(body);
          } else {
            
            commBody.add(body);
          } 
        } 
        SaleOutVO addVO = null;
        if (!ValueCheckUtil.isNullORZeroLength(commBody)) {
          addVO = new SaleOutVO();
          addVO.setParent(vo.getParent());
          addVO.setChildrenVO((CircularlyAccessibleValueObject[])CollectionUtils.listToArray(commBody));
          retList.add(addVO);
        } 
        if (!ValueCheckUtil.isNullORZeroLength(retBody)) {
          addVO = new SaleOutVO();
          addVO.setParent((SaleOutHeadVO)vo.getHead().clone());
          addVO.setChildrenVO((CircularlyAccessibleValueObject[])CollectionUtils.listToArray(retBody));
          retList.add(addVO);
        } 
      } 
    }  return (SaleOutVO[])CollectionUtils.listToArray(retList);
  }
}
